// Trade-Policy Dynamics: Evidence from 60 Years of U.S.-China Trade
// Alessandria,  Khan, Khederlarian, Ruhl, and Steinberg

// inputs:	firm_panel.dta

// outputs:	exporter_moments.dta

// This file uses firm_panel.dta, which is not available in this replication package.
// See the readme file for more information. The exporter_moments.dta file, the output
// of this code, is in  "Raw data confidential/Intermediate files/" directory. 

clear all 
set varabbrev off

global dir_raw "Raw data/"
global dir_int "Intermediate files/"
timer on 1

use "${dir_raw}firmpanel.dta", clear

rename icode ind_cni
g ind_cni_desc = ""
replace ind_cni_desc="Manufacturing of agricultural and non-staple foodstuff" if ind_cni==13
replace ind_cni_desc="Foodstuff manufacturing industry" if ind_cni==14
replace ind_cni_desc="Beverage manufacturing industry" if ind_cni==15
replace ind_cni_desc="Tobacco industry" if ind_cni==16
replace ind_cni_desc="Textile industry" if ind_cni==17
replace ind_cni_desc="Manufacturing industry of textile costumes, shoes, and caps" if ind_cni==18
replace ind_cni_desc="Manufacturing industry of leather, fur, feather (cloth with soft nap) and their products" if ind_cni==19
replace ind_cni_desc="Wood processing and manufacturing industry of wood, bamboo, rattan, palm, and straw-made articles" if ind_cni==20
replace ind_cni_desc="Cabinetmaking industry" if ind_cni==21
replace ind_cni_desc="Papermaking and paper product industry" if ind_cni==22
replace ind_cni_desc="Printing industry and reproduction of record media" if ind_cni==23
replace ind_cni_desc="Manufacturing industry for culture, education and sports goods" if ind_cni==24
replace ind_cni_desc="Petroleum processing, coking and nuclear fuel manufacture" if ind_cni==25
replace ind_cni_desc="Chemical feedstock and chemical manufacturing industry" if ind_cni==26
replace ind_cni_desc="Medicine manufacturing industry" if ind_cni==27
replace ind_cni_desc="Chemical fiber manufacturing industry" if ind_cni==28
replace ind_cni_desc="Rubber production industry" if ind_cni==29
replace ind_cni_desc="Plastic industry" if ind_cni==30
replace ind_cni_desc="Non-metallic minerals product industry" if ind_cni==31
replace ind_cni_desc="Ferrous metal smelting and extrusion" if ind_cni==32
replace ind_cni_desc="Non-ferrous smelting and extrusion" if ind_cni==33
replace ind_cni_desc="Metalwork industry" if ind_cni==34
replace ind_cni_desc="General-purpose equipment manufacturing industry" if ind_cni==35
replace ind_cni_desc="Specialized facility manufacturing industry" if ind_cni==36
replace ind_cni_desc="Transport and communication facilities manufacturing industry" if ind_cni==37
replace ind_cni_desc="Electric machinery and equipment manufacturing industry" if ind_cni==39
replace ind_cni_desc="Manufacturing industry of communication equipment, computers and other electronic equipment" if ind_cni==40
replace ind_cni_desc="Manufacturing industry of instruments and meters, and machinery for culture and office" if ind_cni==41
replace ind_cni_desc="Artwork and other manufacturing industries" if ind_cni==42

g sector_n = .
replace sector_n=1 if ind_cni==13 | ind_cni==14 | ind_cni==15 | ind_cni==16
replace sector_n=2 if ind_cni==17 | ind_cni==18 | ind_cni==19
replace sector_n=3 if ind_cni==20
replace sector_n=4 if ind_cni==22 | ind_cni==23
replace sector_n=5 if ind_cni==25 | ind_cni==26 | ind_cni==27 | ind_cni==28
replace sector_n=6 if ind_cni==29 | ind_cni==30
replace sector_n=7 if ind_cni==31
replace sector_n=8 if ind_cni==32 | ind_cni==33
replace sector_n=9 if ind_cni==34
replace sector_n=10 if ind_cni==35 | ind_cni==36
replace sector_n=11 if ind_cni==40 | ind_cni==41
replace sector_n=12 if ind_cni==39
replace sector_n=13 if ind_cni==37
replace sector_n=14 if ind_cni==21 | ind_cni==24 | ind_cni==42 | ind_cni==16

g sector = ""
replace sector="Food, beverage and tobacco" if sector_n==1
replace sector="Textile, clothing, leather and footwear manufacturing" if sector_n==2
replace sector="Wood and straw products" if sector_n==3
replace sector="Paper and printing products" if sector_n==4
replace sector="Energy products and chemicals" if sector_n==5
replace sector="Rubber and plastic products" if sector_n==6
replace sector="Non-metallic mineral products" if sector_n==7
replace sector="Base metal manufacturing" if sector_n==8
replace sector="Calendered metal manufacturing" if sector_n==9
replace sector="Other machinery and equipment manufacturing industry" if sector_n==10
replace sector="Computer, electronic and optical products" if sector_n==11
replace sector="Electrical equipment manufacturing" if sector_n==12
replace sector="Vehicle manufacturing" if sector_n==13
replace sector="Furniture and other manufacturing" if sector_n==14
replace sector="Non-manufacturing" if sector_n==.

replace export=. if export==0
g export_intens = export/sales
g exporter = export!=.
g temp=1
bysort sector yr: egen tempexporters=sum(exporter)
bysort sector yr: egen tempfirms=sum(temp)
g export_partic = tempexporters/tempfirms
bysort yr: egen tempexporters2=sum(exporter)
bysort yr: egen tempfirms2=sum(temp)
g export_partic_agg = tempexporters2/tempfirms2
egen idn=group(id)
xtset idn yr
g starter = L.exporter==0 & exporter==1
g stopper = L.exporter==1 & exporter==0
bysort sector yr: egen temptot=sum(exporter)
bysort sector yr: egen tempstop=sum(stopper)
bysort sector yr: egen tempstart=sum(starter)
g exit_rate = tempstop/temptot
g entry_rate = tempstart/temptot
bysort yr: egen temptot2=sum(exporter)
bysort yr: egen tempstop2=sum(stopper)
bysort yr: egen tempstart2=sum(starter)
g exit_rate_agg = tempstop2/temptot2
g entry_rate_agg = tempstart2/temptot2
bysort sector yr: egen tempavginc = mean(export/(exporter==1 & starter==0))
bysort sector yr: egen tempavgstart = mean(export/(starter==1))
g incumbent_premium = tempavginc/tempavgstart
bysort yr: egen tempavginc2 = mean(export/(exporter==1 & starter==0))
bysort yr: egen tempavgstart2 = mean(export/(starter==1))
g incumbent_premium_agg = tempavginc2/tempavgstart2
bysort sector yr: egen tempavg = mean(export/(exporter==1)) 
bysort sector yr: egen tempsd = sd(export/(exporter==1)) 
g cov_lexports = log(tempsd/tempavg)
drop tempsd tempavg
g templp = export/totalworker
bysort sector yr: egen tempavg = mean(templp/(exporter==1)) 
bysort sector yr: egen tempsd = sd(templp/(exporter==1)) 
g cov_lexpxl = log(tempsd/tempavg)
drop tempsd tempavg
bysort yr: egen tempavg = mean(export/(exporter==1)) 
bysort yr: egen tempsd = sd(export/(exporter==1)) 
g cov_lexports_agg = log(tempsd/tempavg)
drop tempsd tempavg
bysort yr: egen tempavg = mean(templp/(exporter==1)) 
bysort yr: egen tempsd = sd(templp/(exporter==1)) 
g cov_lexpxl_agg = log(tempsd/tempavg)

loc t 2004
egen temptag=tag(sector yr)
tab sector if temptag==1 & yr>`t', sum(export_partic)
tab sector if yr>`t', sum(export_intens)
tab sector if temptag==1 & yr>`t', sum(entry_rate)
tab sector if temptag==1 & yr>`t', sum(exit_rate) 
tab sector if yr>`t', sum(incumbent_premium) 
tab sector if temptag==1 & yr>`t', sum(cov_lexports) 
tab sector if temptag==1 & yr>`t', sum(cov_lexpxl) 
drop temp*

foreach x in export_intens incumbent_premium {
	bysort sector: egen `x'_0507 = mean(`x'/(yr>2004))
	bysort sector: egen `x'_0407 = mean(`x'/(yr>2003))
	bysort sector: egen `x'_0406 = mean(`x'/(yr>2003 & yr<2007))
	egen `x'_agg_0507 = mean(`x'/(yr>2004))
	egen `x'_agg_0407 = mean(`x'/(yr>2003))
	egen `x'_agg_0406 = mean(`x'/(yr>2003 & yr<2007))
}
egen temptag=tag(sector yr)
foreach x in export_partic entry_rate exit_rate cov_lexports cov_lexpxl {
	bysort sector: egen `x'_0507 = mean(`x'/(yr>2004 & temptag==1))
	bysort sector: egen `x'_0407 = mean(`x'/(yr>2003 & temptag==1))
	bysort sector: egen `x'_0406 = mean(`x'/(yr>2003 & yr<2007 & temptag==1))
}
egen temptag2=tag(yr)
foreach x in export_partic entry_rate exit_rate cov_lexports cov_lexpxl {
	egen `x'_agg_0507 = mean(`x'_agg/(yr>2004 & temptag2==1))
	egen `x'_agg_0407 = mean(`x'_agg/(yr>2003 & temptag2==1))
	egen `x'_agg_0406 = mean(`x'_agg/(yr>2003 & yr<2007 & temptag2==1))
}
g temp=1
bysort sector: egen numfirms_0507 = sum(temp/(yr>2004))
bysort sector: egen numfirms_0407 = sum(temp/(yr>2003))
bysort sector: egen numfirms_0406 = sum(temp/(yr>2003 & yr<2007))
drop temptag
egen temptag=tag(sector)
drop if temptag==0
keep sector* *_0507 *_0407 *_0406
save "${dir_int}exporter_moments.dta", replace
